package cn.iocoder.yudao.module.system.controller.admin.workstroerecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.workstroerecord.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workstroerecord.WorkStroeRecordDO;
import cn.iocoder.yudao.module.system.service.workstroerecord.WorkStroeRecordService;

@Tag(name = "管理后台 - 入库托数记录")
@RestController
@RequestMapping("/system/work-stroe-record")
@Validated
public class WorkStroeRecordController {

    @Resource
    private WorkStroeRecordService workStroeRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建入库托数记录")
    @PreAuthorize("@ss.hasPermission('system:work-stroe-record:create')")
    public CommonResult<Long> createWorkStroeRecord(@Valid @RequestBody WorkStroeRecordSaveReqVO createReqVO) {
        return success(workStroeRecordService.createWorkStroeRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新入库托数记录")
    @PreAuthorize("@ss.hasPermission('system:work-stroe-record:update')")
    public CommonResult<Boolean> updateWorkStroeRecord(@Valid @RequestBody WorkStroeRecordSaveReqVO updateReqVO) {
        workStroeRecordService.updateWorkStroeRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除入库托数记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-stroe-record:delete')")
    public CommonResult<Boolean> deleteWorkStroeRecord(@RequestParam("id") Long id) {
        workStroeRecordService.deleteWorkStroeRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除入库托数记录")
                @PreAuthorize("@ss.hasPermission('system:work-stroe-record:delete')")
    public CommonResult<Boolean> deleteWorkStroeRecordList(@RequestParam("ids") List<Long> ids) {
        workStroeRecordService.deleteWorkStroeRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得入库托数记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-stroe-record:query')")
    public CommonResult<WorkStroeRecordRespVO> getWorkStroeRecord(@RequestParam("id") Long id) {
        WorkStroeRecordDO workStroeRecord = workStroeRecordService.getWorkStroeRecord(id);
        return success(BeanUtils.toBean(workStroeRecord, WorkStroeRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得入库托数记录分页")
    @PreAuthorize("@ss.hasPermission('system:work-stroe-record:query')")
    public CommonResult<PageResult<WorkStroeRecordRespVO>> getWorkStroeRecordPage(@Valid WorkStroeRecordPageReqVO pageReqVO) {
        PageResult<WorkStroeRecordRespVO> pageResult = workStroeRecordService.getWorkStroeRecordPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出入库托数记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:work-stroe-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkStroeRecordExcel(@Valid WorkStroeRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkStroeRecordRespVO> list = workStroeRecordService.getWorkStroeRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "入库托数记录.xls", "数据", WorkStroeRecordRespVO.class,
                        BeanUtils.toBean(list, WorkStroeRecordRespVO.class));
    }

}
